<template>
  <el-container class="home_container">
  <!-- 头部区域 -->
  <el-header height="80px">
      <!-- logo -->
      <div>
        <img src="../../assets/homelogo.png" alt="">
      </div>
      <!-- 退出登陆按钮 -->
      <div class="user">
      <!-- 管理员:<span class="username">{{name}}</span> -->
      <el-button
        type="info"
        @click="logout"
      >退出</el-button>
      </div>
  </el-header>
  <!-- 页面主体区域 -->
  <el-container>
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '65px' : '200px'">
      <!-- 折叠开关 -->
      <div
        class="toggle_button"
        @click="toggleCollapse"
        >|||</div>
      <!-- 菜单列表 -->
      <el-menu
      router
      :collapse-transition="false"
      :collapse="isCollapse"
      unique-opened
      :default-active="activePath"
      class="el-menu-vertical-demo"
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409eff">
      <!-- 一级菜单 -->
      <el-submenu
        :index="item.id  + ''"
        v-for="item in menuList"
        :key="item.id"
        >
        <template slot="title">
          <i :class="iconObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
        <el-menu-item
          :index="'/' + subItem.path"
          v-for="subItem in item.children"
          :key="subItem.id"
          @click="saveNavState('/' + subItem.path)"
        >
          <template slot="title">
            <i class="el-icon-menu"></i>
            <span>{{subItem.authName}}</span>
          </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <!-- 右侧主体 -->
    <el-main>
      <!-- 路由占位符 -->
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  namg: 'Home',
  data () {
    return {
      menuList: [],
      iconObj: {
        125: 'iconfont icon-yonghuguanli',
        103: 'iconfont icon-quanxian',
        101: 'iconfont icon-xiazai',
        102: 'iconfont icon-dingdanguanli',
        145: 'iconfont icon-tongji2x',
        160: 'iconfont icon-tongji2x',
        161: 'iconfont icon-tongji2x',
        162: 'iconfont icon-tongji2x',
        163: 'iconfont icon-tongji2x',
        164: 'iconfont icon-tongji2x'
      },
      isCollapse: false,
      activePath: '',
      name: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登陆
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
      this.$message.success({
        message: '退出成功',
        duration: 1500
      })
    },
    // 获取菜单列表
    async getMenuList () {
      const { data: res } = await this.$http('menus')
      if (res.meta.status !== 200) return this.$message.error('获取菜单列表失败')
      this.menuList = res.data
    },
    // 折叠菜单列表
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  padding-left: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  img {
    width: 200px;
    height: 100%;
  }

  .user {
    color: #fff;
    .username {
      margin-right: 10px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .toggle_button {
    background-color: #495163;
    font-size: 12px;
    color: #fff;
    text-align: center;
    cursor: pointer;
    line-height: 30px;
  }
  .el-menu {
    border-right: 0;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
</style>
